/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.chenweihuacwh.web.orderManage;

import java.util.List;
import java.util.Set;

import com.javaeye.chenweihuacwh.dto.baseData.Product;
import com.javaeye.chenweihuacwh.dto.orderManage.PurchaseOrder;
import com.javaeye.chenweihuacwh.dto.orderManage.PurchaseOrderDetail;
import com.javaeye.chenweihuacwh.service.baseData.ICodeService;
import com.javaeye.chenweihuacwh.service.baseData.ProductCategoryService;
import com.javaeye.chenweihuacwh.service.baseData.ProductService;
import com.javaeye.chenweihuacwh.service.orderManage.IPurchaseOrderService;
import com.javaeye.chenweihuacwh.util.ListItem;
import com.javaeye.chenweihuacwh.util.ListUtil;
import com.javaeye.chenweihuacwh.web.PageInfo;
import com.opensymphony.xwork2.ActionSupport;

public class PurchaseOrderAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7408989332825417843L;
	
	private OrderCondition condition;
	
	private PageInfo pageInfo;
	
	private List<PurchaseOrder> orderList;
	
	private String orderId;
	
	/**
	 * 单位列表
	 */
	private List<ListItem> unitList;
	
	/**
	 * 订单信息
	 */
	private PurchaseOrder order;
	
	/**
	 * 订单明细ID
	 */
	private List<Integer> detailIds;
	
	/**
	 * 订单明细产品ID
	 */
	private List<String> productIds;
	
	/**
	 * 订单明细产品名称
	 */
	private List<String> productNames;
	
	/**
	 * 订单明细产品数量
	 */
	private List<Integer> productNums;
	
	/**
	 * 订单明细产品单位
	 */
	private List<String> productUnits;
	
	/**
	 * 订单明细产品价格
	 */
	private List<Double> productPrices;
	
	/**
	 * 订单明细其他费用
	 */
	private List<Double> otherAmounts;
	
	/**
	 * 订单明细金额
	 */
	private List<Double> productAmounts;
	
	private IPurchaseOrderService service;
	
	private ProductService productService;
	
	private ProductCategoryService productCategoryService;
	
	private ICodeService codeService;
	
	public PurchaseOrderAction() {
		pageInfo = new PageInfo();
		pageInfo.setPageIndex(0);
		condition = new OrderCondition();
		condition.setCustomName("");
		condition.setProductName("");
	}
	
	public void setCodeService(ICodeService codeService) {
		this.codeService = codeService;
	}

	/**
	 * 查询用于增加新订单
	 * @return
	 * @throws Exception
	 */
	public String queryForAddPurchaseOrder() throws Exception {
		unitList = ListUtil.unitList(codeService);
		return SUCCESS;
	}
	
	/**
	 * 查询订单列表
	 * @return
	 * @throws Exception
	 */
	public String queryPurchaseOrderList() throws Exception {
		orderList = service.getPurchaseOrderList(condition, pageInfo);
		return SUCCESS;
	}
	
	/**
	 * 查询订单信息用于编辑
	 * @return
	 * @throws Exception
	 */
	public String queryPurchaseOrderForEdit() throws Exception {
		unitList = ListUtil.unitList(codeService);
		order = service.getPurchaseOrderInfo(orderId);
		return SUCCESS;
	}
	
	/**
	 * 查询订单信息
	 * @return
	 * @throws Exception
	 */
	public String queryPurchaseOrder() throws Exception {
		order = service.getPurchaseOrderInfo(orderId);
		Set<PurchaseOrderDetail> details = order.getOrderDetails();
		for (PurchaseOrderDetail detail : details) {
			detail.setUnit(ListUtil.unitName(codeService, detail.getUnit()));
		}
		return SUCCESS;
	}
	
	/**
	 * 删除订单信息
	 * @return
	 * @throws Exception
	 */
	public String removePurchaseOrder() throws Exception {
		service.removePurchaseOrder(orderId);
		return SUCCESS;
	}
	
	/**
	 * 保存出货单
	 * @return
	 * @throws Exception
	 */
	public String savePurchaseOrder() throws Exception {
		for (int i = 0; i < productIds.size(); i++) {
			String productId = productIds.get(i);
			Product product = productService.getProduct(productId);
			
			PurchaseOrderDetail detail = new PurchaseOrderDetail();
			detail.setProductId(product.getId());
			detail.setProductName(product.getName());
			detail.setProductType(product.getType());
			detail.setProductCategory(productCategoryService.getCategory(product.getCategory()).getName());
			detail.setProductNo(product.getProductNo());
			detail.setCustomNo(product.getCustomerNo());
			detail.setNumber(productNums.get(i));
			detail.setUnit(productUnits.get(i));
			detail.setPrice(productPrices.get(i));
			detail.setOtherAmount(otherAmounts.get(i));
			detail.setAmount(productAmounts.get(i));
			
			if (detailIds != null && detailIds.size() > 0) {
				if (i < detailIds.size()) {
					detail.setId(detailIds.get(i));
				}
			}
			
			order.addDetail(detail);
		}
		service.savePurchaseOrder(order);
		return SUCCESS;
	}

	public OrderCondition getCondition() {
		return condition;
	}

	public void setCondition(OrderCondition condition) {
		this.condition = condition;
	}

	public PageInfo getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(PageInfo pageInfo) {
		this.pageInfo = pageInfo;
	}

	public List<PurchaseOrder> getOrderList() {
		return orderList;
	}

	public void setOrderList(List<PurchaseOrder> orderList) {
		this.orderList = orderList;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public PurchaseOrder getOrder() {
		return order;
	}

	public void setOrder(PurchaseOrder order) {
		this.order = order;
	}

	public List<String> getProductIds() {
		return productIds;
	}

	public void setProductIds(List<String> productIds) {
		this.productIds = productIds;
	}

	public List<String> getProductNames() {
		return productNames;
	}

	public void setProductNames(List<String> productNames) {
		this.productNames = productNames;
	}

	public List<Integer> getProductNums() {
		return productNums;
	}

	public void setProductNums(List<Integer> productNums) {
		this.productNums = productNums;
	}

	public List<Double> getProductPrices() {
		return productPrices;
	}

	public void setProductPrices(List<Double> productPrices) {
		this.productPrices = productPrices;
	}

	public List<Double> getProductAmounts() {
		return productAmounts;
	}

	public void setProductAmounts(List<Double> productAmounts) {
		this.productAmounts = productAmounts;
	}

	public void setService(IPurchaseOrderService service) {
		this.service = service;
	}

	public void setDetailIds(List<Integer> detailIds) {
		this.detailIds = detailIds;
	}

	public List<Integer> getDetailIds() {
		return detailIds;
	}

	public void setProductUnits(List<String> productUnits) {
		this.productUnits = productUnits;
	}

	public List<String> getProductUnits() {
		return productUnits;
	}

	public void setUnitList(List<ListItem> unitList) {
		this.unitList = unitList;
	}

	public List<ListItem> getUnitList() {
		return unitList;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public void setProductCategoryService(
			ProductCategoryService productCategoryService) {
		this.productCategoryService = productCategoryService;
	}

	/**
	 * @param otherAmounts the otherAmounts to set
	 */
	public void setOtherAmounts(List<Double> otherAmounts) {
		this.otherAmounts = otherAmounts;
	}

	/**
	 * @return the otherAmounts
	 */
	public List<Double> getOtherAmounts() {
		return otherAmounts;
	}
	
}
